class User < ActiveRecord::Base
    has_many :comments
    has_many :posts
    has_many :owners
    has_many :blogs, through: :owners, dependent: :delete_all

    #attr_accessor :password

    email_regex = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]+)\z/i

    validates :name, presence: true, length: {maximum: 50}
    validates :email, presence: true, format: {with: email_regex}, uniqueness: {case_sensitive: false}
    validates :password, presence: true, confirmation: true, length: {within: 6..40}
    before_save :encrypt_password

    def has_password?(submitted_password)
        password == encrypt(submitted_password)
    end

    def self.authenticate(email, submitted_password)
        user = find_by_email(email)
        return nil if user.nil?
        return user if user.has_password?(submitted_password)
    end

    private
        def encrypt_password
            # generate unique salt if new user
            self.salt = Digest::SHA2.hexdigest("#{Time.now.utc}--#{password}") if self.new_record?

            # encrypt the password and store 
            self.password = encrypt(password)
        end

        def encrypt(pass)
            Digest::SHA2.hexdigest("#{self.salt}--#{pass}")
        end
end
